package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.PageParam;
import com.itheima.bean.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import com.itheima.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //新增套餐
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){

        boolean result = setmealService.add(setmealDto);

        if(result){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    //分页查询套餐数据
    @GetMapping("/page")
    public R page(PageParam pageParam){
        IPage<SetmealDto> page = setmealService.page(pageParam);

        return R.success(page);
    }

    //删除套餐
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        boolean result = setmealService.delete(ids);

        if(result){
            return R.success("删除套餐成功");
        }
        return R.error("删除套餐失败");
    }

    //根据分类来查询套餐数据
    @GetMapping("/list")
    public R list(Long categoryId, Integer status){
        //1.构建条件对象
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        //2.封装条件
        lqw.eq(Setmeal::getCategoryId,categoryId);
        lqw.eq(Setmeal::getStatus,status);

        //3.执行查询
        List<Setmeal> list = setmealService.list(lqw);

        //4.返回结果
        return R.success(list);
    }
}
